package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	
	private static MainFrame instance = null;

	ToolbarStandard myToolbar;
	VerticalToolbar vertToolbar;
	MyImagePanel srednji;
	String path="mandarine.jpg";
	File image;
	 final JLabel selectionLabel = new JLabel(
             "Drag a rectangle in the screen shot!");
	
	
	private MainFrame()	{
		super();
	} 
	
	public static MainFrame getInstance() {
		if (instance == null) {
			instance = new MainFrame();
			instance.initGUI();
		}
		
		return instance;
	}
	
	public void initGUI() {
		
		Toolkit kit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = kit.getScreenSize();
	    int screenHeight = screenSize.height;
	    int screenWidth = screenSize.width;
		
        setSize(3*screenWidth / 4, 3*screenHeight / 4);
        setTitle("OCR");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myToolbar = new ToolbarStandard();
		Color myColor = new Color(176,196,222);
		myToolbar.setBackground(myColor);
		add(myToolbar, BorderLayout.NORTH);
        
		vertToolbar = new VerticalToolbar();
		vertToolbar.setBackground(myColor);
		add(vertToolbar, BorderLayout.EAST);
		
		
		 add(selectionLabel,BorderLayout.SOUTH);
       
		srednji = new MyImagePanel();
		add(srednji, BorderLayout.CENTER);
		setLocationRelativeTo(null);
	}
	
	class MyImagePanel extends JPanel implements MouseMotionListener, MouseListener{
		
		BufferedImage img;
		int x;
		int y;
		
		int cx;
		int cy;
		
		int dx;
		int dy;
		
		Point point;
		
		Rectangle captureRect;
		JLabel screenLabel;
		BufferedImage screenCopy;
		BufferedImage newImg;
		
		public MyImagePanel(){
			addMouseMotionListener(this);
			addMouseListener(this);
		image = new File(path);
		
		
		try{
		img = ImageIO.read(image);
		}catch(Exception e){
		}
		
		x=0;
		y=0;
		
		newImg = new BufferedImage(
				img.getWidth(),
				img.getHeight(),
				img.getType());
		screenCopy = new BufferedImage(
				img.getWidth(),
				img.getHeight(),
				img.getType());
        screenLabel = new JLabel(new ImageIcon(screenCopy));
        JScrollPane screenScroll = new JScrollPane(screenLabel);

        screenScroll.setPreferredSize(new Dimension(
                (int)(img.getWidth()),
                (int)(img.getHeight())));

        JPanel panel = new JPanel(new BorderLayout());
        add(screenLabel, BorderLayout.CENTER);

      //  final JLabel selectionLabel = new JLabel(
             //   "Drag a rectangle in the screen shot!");
        //add(selectionLabel, BorderLayout.SOUTH);

        repaint(img, screenCopy);
        screenLabel.repaint();

        screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

            Point start = new Point();

            @Override
            public void mouseMoved(MouseEvent me) {
                start = me.getPoint();
                
                repaint(img, screenCopy);
                selectionLabel.setText("Start Point: " + start);
                screenLabel.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                Point end = me.getPoint();
                captureRect = new Rectangle(start,
                        new Dimension(end.x-start.x, end.y-start.y));
                repaint(img, screenCopy);
                screenLabel.repaint();
                selectionLabel.setText("Rectangle: " + captureRect);
                try{
                newImg = img.getSubimage(start.x, start.y, end.x-start.x, end.y-start.y);
                }
                catch(Exception e){
                	
                }
               ToolbarStandard.img=newImg;
               ToolbarStandard.startingPoint=start;
               ToolbarStandard.endingPoint=end;
            }
           
            
        });
        
        screenLabel.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				point=e.getPoint();
				captureRect = null;
				repaint(img,screenCopy);
				screenLabel.repaint();
				ToolbarStandard.img=img;
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

        //JOptionPane.showMessageDialog(null, panel);

        //System.out.println("Rectangle of interest: " + captureRect);
		
		
		}
		
		
		
		public void repaint(BufferedImage orig, BufferedImage copy) {
	        Graphics2D g = copy.createGraphics();
	        g.drawImage(orig,0,0, null);
	        if (captureRect!=null) {
	            g.setColor(Color.RED);
	            g.draw(captureRect);
	            g.setColor(new Color(255,255,255,150));
	            g.fill(captureRect);
	        }
	        g.dispose();
	    }
		
		public void setImg(BufferedImage img2){
			this.img=img2;
		}
		
		
		
		/*public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(
					img,
					x,
					y,
					null);

		}*/

		public void setScreenCopy(BufferedImage screenCopy) {
			this.screenCopy = screenCopy;
		}



		public BufferedImage getImg() {
			return img;
		}



		public JLabel getScreenLabel() {
			return screenLabel;
		}

		

		public BufferedImage getNewImg() {
			return newImg;
		}



		public void setCaptureRect(Rectangle captureRect) {
			this.captureRect = captureRect;
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
		/*	Point p = e.getPoint();
			dx = p.x;
			dy = p.y;
	 
	 
		if(cx > dx)
			x += (cx - dx) / 100;	
		else
			x -= (cx - dx) / 100;	
	 
		if(cy > dy)
			y += (cy - dy) / 100;
		else
			y -= (cy - dy) / 100;		
	 
		cx = x;
		cy = y;
			repaint();*/
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			//Point p = e.getPoint();
			 
			//cx = p.x;
			//cy = p.y;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		
		
	
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public MyImagePanel getSrednji() {
		return srednji;
	}

	public void setSrednji(MyImagePanel srednji) {
		this.srednji = srednji;
	}
	
	
			

}
